/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.Query;
import jpa.exceptions.NonexistentEntityException;

/**
 *
 * @author Kelompok 3
 * Kelas ini berfungsi untuk mengelola fungsi yang dibutuhkan dalam modul Payment
 * Fungsi tersebut dihungunkan dengan kelas entitas HistoryPayment yang merupakan represntasi dari tabel payments dalam database
 */
public class HistoryPaymentQuery {
    /* menghubungkan dengan database sesuai dengan 
     * konfigurasi pada persistence unit
     */

    public HistoryPaymentQuery() {
        emf = Persistence.createEntityManagerFactory("persistence");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public List<HistoryPayment> getHistoryPayments(Event event) {
        List<HistoryPayment> historyPayments = new ArrayList<HistoryPayment>();

        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT object(o) FROM HistoryPayment AS o WHERE o.idEvent=:ie ORDER BY o.idPayment DESC");
            q.setParameter("ie", event);
            historyPayments = q.getResultList();

        } finally {
            if (em != null) {
                em.close();
            }
        }
        return historyPayments;
    }

    /* method untuk memasukkan satu baris Payment baru ke dalam tabel historyPayments
     */
    public void createHistoryPayment(HistoryPayment historyPayment) {
        EntityManager em = getEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(historyPayment);
            em.getTransaction().commit();
        } catch (Exception e) {
            em.getTransaction().rollback();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    /* method untuk menghapus satu baris Payment pada tabel historyPayments
     */
    public void deleteHistoryPayment(int id) throws NonexistentEntityException {
        EntityManager em = getEntityManager();
        em.getTransaction().begin();
        try {
            HistoryPayment historyPayment;
            try {
                historyPayment = em.getReference(HistoryPayment.class, id);
                historyPayment.getIdPayment();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The historyPayment with id " + id + " no longer exists.", enfe);
            }
            em.remove(historyPayment);
            em.getTransaction().commit();
        } catch (Exception e) {
            em.getTransaction().rollback();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }
}
